<?php

/*
 * 		SEARCH FORM
 */

class Inc_Form_Search extends Zend_Form
{
	public $messages = array(
			Zend_Validate_Int::INVALID => '\'%value%\' is not en integer',
			Zend_Validate_Int::NOT_INT => '\'%value%\' is not en integer'
			);
	
	
	public function init()
	{
		/*
		 * 		INIT FORM
		 */
		$this->setAction('/catalog/item/search')->setMethod('get');

		/*
		 * 		SET DECORATOR
		 */
		$this->setDecorators(array(
				array('FormErrors', 
						array('markupListItemStart' => '',
							  'markupListItemEnd' => '')),
				array('FormElements'),
				array('Form')));
		
		/*
		 * 		SEARCH YEAR	
		 */
		$year = new Zend_Form_Element_Text('y');
		$year->setLabel('Year: ')
			->setOptions(array('size' => '6'))
			->addValidator('Int', false, array('messages' => $this->messages))
			->addFilter('HtmlEntities')
			->addFilter('StringTrim');
		
		/*
		 * 		SEARCH PRICE
		 */
		$price = new Zend_Form_Element_Text('p');
		$price->setLabel('Price: ')
			->setOptions(array('size' => '8'))
			->addValidator('Int', false, array('messages' => $this->messages))
			->addFilters(array('HtmlEntities', 'StringTrim'));
	
		/*
		 * 		SEARCH GRADE
		 */
		$grade = new Zend_Form_Element_Select('g');
		$grade->setLabel('Grade: ')
		->addValidator('Int', false, array('messages' => $this->messages))
		->addFilters(array('HtmlEntities', 'StringTrim'))
		->addMultiOption('', 'Any');
		
		foreach ($this->getGrades() as $g)
		{
			$grade->addMultiOption($g['GradeID'], $g['GradeName']);
		};
		
		/*
		 * 		SUBMIT BUTTON
		*/
		
		$submit = new Zend_Form_Element_Submit('submit');
		$submit->setLabel('Search');
		 
		/*
		 * 		ADD ELEMENTS FORM
		 */
		$this->addElement($year)
			 ->addElement($price)
		  	 ->addElement($grade)
			 ->addElement($submit);
		/*
		 * 		ADD DECORATORS
		 */
		$this->setElementDecorators(array(
				array('ViewHelper'),
				array('Label' , array('tag' => '<span>'))
		));
		
		$submit->setDecorators(array(
				array('ViewHelper')));
		
	}
	
	public function getGrades()
	{
		$query = Doctrine_Query::create()
			->from('Zend_Model_Grade g');		
		return $query->fetchArray();
	}
}